package com.knight.tomcat.common;

import java.io.IOException;
import java.io.InputStream;

public class KnightRequest {
    private InputStream inputStream;
    private String method;
    private String url;


    public KnightRequest(InputStream inputStream) {
        this.inputStream = inputStream;

        byte[] buffer = new byte[1024];

        try {
            int len = inputStream.read(buffer);
            if (len > 0) {
                String content = new String(buffer, 0, len);
                System.out.println("http request:" + content);
                if (content != null && content.length() > 0) {
                    String[] lines = content.split("\\r\\n");
                    String[] fields = lines[0].split("\\s");
                    method = fields[0];
                    url = fields[1];
                }

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String getMethod() {
        return method;
    }

    public String getUrl() {
        return url;
    }

    @Override
    public String toString() {
        return "KnightRequest{" +
                ", method='" + method + '\'' +
                ", url='" + url + '\'' +
                '}';
    }
}
